import React, { Component } from "react";

export default class Item extends Component {
  state = {
    mouse: false,
  };
  //鼠标事件
  handleMouse = (status) => {
    return (event) => {
      //鼠标移入事件
      if (status === "enter") {
        this.setState({ mouse: true });
      } else {
        this.setState({ mouse: false });
      }
    };
  };
  change = (event) => {
    console.log(this.props.todo.id,event.target.checked);
    const { todo } = this.props;
    todo.done = event.target.checked.toString()
    this.props.changeChecked(todo)
  };
  //删除事件
  deleteLi = ()=>{
    this.props.deleteLi(this.props.todo.id)
  }
  render() {
    const { todo } = this.props;
    const { mouse } = this.state;
    return (
      <li
        onMouseEnter={this.handleMouse("enter")}
        onMouseLeave={this.handleMouse("leave")}
        style={{ backgroundColor: mouse ? "#ddd" : "#fff" }}
      >
        <label>
          <input
            type="checkbox"
            defaultChecked={todo.done === "true"}
            onChange={this.change}
          />
          <span>{todo.name}</span>
        </label>
        <button
          className="btn btn-danger"
          style={{ display: mouse ? "inline-block" : "none" }}
          onClick = {this.deleteLi}
        >
          删除
        </button>
      </li>
    );
  }
}
